﻿using NikeSale.Models;
using static NikeSale.Bll.DBSelect;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin.SkinControl;

namespace NikeSale.Ull
{
    public partial class GoodsWarehousForm : Form
    {
        public GoodsWarehousForm()
        {
            InitializeComponent();
            this.tb_StockNum.SkinTxt.KeyPress += SkinTxt_NumberCheck_KeyPress;
            this.tb_Discount.SkinTxt.KeyPress += SkinTxt_DecimalCheck_KeyPress;
            this.tb_ArticleNo.SkinTxt.KeyPress += SkinTxt_NumberCheck_KeyPress;
            this.tb_PurchasePrice.SkinTxt.KeyPress += SkinTxt_DecimalCheck_KeyPress;
            this.tb_WholesalePrice.SkinTxt.KeyPress += SkinTxt_DecimalCheck_KeyPress;

            this.cb_Parent.Items.AddRange(GoodsClassIfication.GetParentTypes());
            this.cb_Parent.SelectedIndex = 0;
        }

        private void SkinTxt_NumberCheck_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == (int)Keys.Back) return;
            if (char.IsNumber(e.KeyChar)) return;
            e.Handled = true;
        }

        private void SkinTxt_DecimalCheck_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == (int)Keys.Decimal) return;
            if ((int)e.KeyChar == (int)Keys.Delete) return;
            if ((int)e.KeyChar == (int)Keys.Back) return;
            if (char.IsNumber(e.KeyChar)) return;
            e.Handled = true;
        }

        private void bt_ReadInfo_Click(object sender, EventArgs e)
        {
            if (this.tb_ArticleNo.Text.Length != 6)
            {
                Methods.ShowPrompt("货号格式不合法");
                return;
            }
            Goods gds = GoodsWarehousing.GetGoodsInfo(this.tb_ArticleNo.Text);
            if (gds == null)
            {
                Methods.ShowPrompt(" 货号不存在,此次入库操作将添加此商品", 2000);
                this.ClearText();
                return;
            }

            this.tb_GoodsName.Text = gds.GoodsName;
            this.tb_Discount.Text = gds.Discount;
            this.lb_Surplus.Text = "当前库存:" + gds.StockNum;
            this.tb_PurchasePrice.Text = gds.StorePrice;
            this.tb_WholesalePrice.Text = gds.SalePrice;
            string pID = GoodsClassIfication.GetParentTypeID(gds.GoodsTypeName);
            this.SetType(pID, gds.GoodsTypeName);
        }

        private void bt_WarehousingClick(object sender, EventArgs e)
        {
            #region 条件过滤
            if (this.tb_ArticleNo.Text.Length != 6)
            {
                Methods.ShowPrompt("货号格式不合法");
                return;
            }
            if (this.tb_StockNum.Text.Trim().Length == 0)
            {
                Methods.ShowPrompt("入库数量为空!");
                return;
            }
            if (this.tb_GoodsName.Text.Trim().Length == 0)
            {
                Methods.ShowPrompt("商品名称为空!");
                return;
            }
            if (this.tb_Discount.Text.Trim().Length==0)
            {
                Methods.ShowPrompt("折扣为空!");
                return;
            }
            if (this.tb_PurchasePrice.Text.Trim().Length==0)
            {
                Methods.ShowPrompt("进货价格为空!");
                return;
            }
            if (this.tb_WholesalePrice.Text.Trim().Length==0)
            {
                Methods.ShowPrompt("零售价格为空!");
                return;
            }
            #endregion

            if (GoodsWarehousing.HasGoods(this.tb_ArticleNo.Text))
            {
                GoodsWarehousing.AddStockNum(this.tb_StockNum.Text, this.tb_ArticleNo.Text);
            }
            else
            {
                Goods gs = new Goods();
                gs.BarCode = this.tb_ArticleNo.Text;
                gs.Discount = this.tb_Discount.Text;
                gs.GoodsName = this.tb_GoodsName.Text;
                gs.GoodsTypeID = ((ComboxItem)this.cb_Sub.Items[this.cb_Sub.SelectedIndex]).ID;
                gs.SalePrice = this.tb_WholesalePrice.Text;
                gs.StockDate = DateTime.Now;
                gs.StockNum = this.tb_StockNum.Text;
                gs.StorePrice = this.tb_PurchasePrice.Text;

                GoodsWarehousing.InsertGoods(gs);
            }
            this.lb_Surplus.Text = "当前库存" + GoodsWarehousing.GetStockNum(this.tb_ArticleNo.Text);
            Methods.ShowPrompt(" 操作完成!", 1000);
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cb_Parent_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetGuideComboxInfo();
            this.cb_Sub.SelectedIndex = 0;
        }

        private void tb_ArticleNo_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = this.bt_ReadInfo;
        }

        private void tb_StockNum_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = this.bt_Warehousing;
        }

        /// <summary>
        /// 设置商品二级下拉列表值
        /// </summary>
        public void SetGuideComboxInfo()
        {
            this.cb_Sub.Items.Clear();
            this.cb_Sub.Items.AddRange(GoodsClassIfication.GetSubTypes(this.cb_Parent.Text));
            if (this.cb_Sub.Items.Count == 0) this.cb_Sub.Items.Add("全部");
        }

        /// <summary>
        /// 设置商品类型
        /// </summary>
        /// <param name="ParentID"></param>
        /// <param name="subTypeName"></param>
        public void SetType(string ParentID,string subTypeName)
        {
            for (int i = 0; i <this.cb_Parent.Items.Count; i++)
            {
                ComboxItem com = (ComboxItem)this.cb_Parent.Items[i];
                if (com.ID==ParentID)
                {
                    this.cb_Parent.SelectedIndex = i;
                    break;
                }
            }
            for (int i = 0; i < this.cb_Sub.Items.Count; i++)
            {
                ComboxItem com = (ComboxItem)this.cb_Sub.Items[i];
                if (com.Text==subTypeName)
                {
                    this.cb_Sub.SelectedIndex = i;
                    break;
                }
            }
        }

        /// <summary>
        /// 清除历史文本
        /// </summary>
        public void ClearText()
        {
            foreach (Control item in this.skinGroupBox8.Controls)
            {
                if (item.Name== "tb_ArticleNo" || !(item is SkinTextBox) )
                {
                    continue;
                }
                item.Text = string.Empty;
            }
            this.lb_Surplus.Text = "当前库存量0";
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = true;
            this.Hide();
        }
    }
}
